<template>
  <div>
    <h1>商品类型</h1>
    <div style="margin-bottom: 10px">
      <el-button type="primary" @click="handleAdd"> 添加 </el-button>
    </div>
    <div>
      <el-table
        :data="typeTree"
        style="width: 100%; margin-bottom: 20px"
        row-key="id"
        border
        default-expand-all
      >
        <el-table-column prop="name" label="名称" />
        <el-table-column prop="typeStatus" label="状态">
          <template #default="scope">
            <el-tag type="success" v-if="scope.row.typeStatus == 0"
              >显示</el-tag
            >
            <el-tag type="danger" v-if="scope.row.typeStatus == 1">隐藏</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="typeTop" label="排序" />
        <el-table-column prop="createTime" label="创建时间" />
        <el-table-column label="类型图片">
          <template #default="scope">
            <el-avatar
              v-if="scope.row.typeImage != null"
              shape="square"
              :src="scope.row.typeImage"
            />
          </template>
        </el-table-column>
        <el-table-column label="操作" width="280px">
          <template #default="scope">
            <el-button size="small" @click="handleEdit(scope.row)">
              编辑
            </el-button>
            <el-button
              size="small"
              type="danger"
              @click="handleDelete(scope.row)"
            >
              删除
            </el-button>
            <el-button
              size="small"
              type="success"
              v-if="scope.row.typeStatus == 1"
              @click="handleChange(scope.row, 0)"
            >
              显示
            </el-button>
            <el-button
              size="small"
              type="danger"
              v-if="scope.row.typeStatus == 0"
              @click="handleChange(scope.row, 1)"
            >
              隐藏
            </el-button>
            <el-button
              size="small"
              type="primary"
              @click="onHandleAddType(scope.row)"
            >
              添加
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
  </div>
  <div>
    <el-dialog v-model="dialogFormVisible" :title="title" width="500">
      <el-form :model="form">
        <el-form-item label="分类名称" :label-width="formLabelWidth">
          <el-input v-model="form.typeName" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类排序" :label-width="formLabelWidth">
          <el-input v-model="form.typeTop" autocomplete="off" />
        </el-form-item>
        <el-form-item label="分类状态" :label-width="formLabelWidth">
          <el-radio-group v-model="form.typeStatus">
            <el-radio :value="0">显示</el-radio>
            <el-radio :value="1">隐藏</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="分类图片" :label-width="formLabelWidth">
          <el-upload
            class="avatar-uploader"
            action="http://localhost:10010/shop/type/uploadFile"
            :headers="headers"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
          >
            <img v-if="imageUrl" :src="imageUrl" class="avatar" />
            <el-icon v-else class="avatar-uploader-icon"><Plus /></el-icon>
          </el-upload>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" v-if="title == '添加类型'" @click="saveAdd">
            提交
          </el-button>
          <el-button
            type="primary"
            v-if="title == '编辑类型'"
            @click="editType"
          >
            编辑
          </el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import {
  getTypeTree,
  setTypeStatus,
  deleteTypeById,
  addType,
  updateType,
} from "@/api/api";
import { ElMessage, ElMessageBox } from "element-plus";
import { onMounted, ref } from "vue";
import { Plus } from "@element-plus/icons-vue";
import type { UploadProps } from "element-plus";

const imageUrl = ref("");
const title = ref("添加类型");
const headers = { token: sessionStorage.getItem("token") };
const typeTree = ref([]);
const dialogFormVisible = ref(false);
const formLabelWidth = "140px";
const form = ref({
  typeId: 0,
  typePid: 0,
  typeName: null,
  typeStatus: 0,
  typeTop: null,
  typeImage: null,
});
const resetForm = () => {
  form.value = {
    typeId: 0,
    typePid: 0,
    typeName: null,
    typeStatus: 0,
    typeTop: null,
    typeImage: null,
  };
  imageUrl.value = "";
};

const handleAvatarSuccess: UploadProps["onSuccess"] = (
  response,
  uploadFile
) => {
  //imageUrl.value = URL.createObjectURL(uploadFile.raw!);
  imageUrl.value = response.data;
};

const handleAdd = () => {
  title.value = "添加类型";
  dialogFormVisible.value = true;
  resetForm();
};
const saveAdd = () => {
  form.value.typeImage = imageUrl.value;

  addType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("添加成功");
      dialogFormVisible.value = false;
      loadTypeTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const onHandleAddType = (row: any) => {
  title.value = "添加类型";
  resetForm();
  form.value.typePid = row.id;
  dialogFormVisible.value = true;
};
const handleEdit = (row: any) => {
  dialogFormVisible.value = true;
  title.value = "编辑类型";
  Object.assign(form.value, row);
  form.value.typeName = row.name;
  form.value.typeId = row.id;
  form.value.typePid = row.parentId;
  imageUrl.value = form.value.typeImage;
  console.log(row);
};
const editType = () => {
  form.value.typeImage = imageUrl.value;
  updateType(form.value).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("编辑成功");
      dialogFormVisible.value = false;
      loadTypeTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
};
const handleDelete = (row: any) => {
  console.log(row);
  ElMessageBox.confirm("删除后数据无法恢复，您确认删除吗?", "警告", {
    confirmButtonText: "确认",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      deleteTypeById(row.id).then((res) => {
        if (res.data.code == 200) {
          ElMessage.success("删除成功");
          loadTypeTree();
        } else {
          ElMessage.error(res.data.msg);
        }
      });
    })
    .catch(() => {});
};
const handleChange = (row: any, status: number) => {
  setTypeStatus(row.id, status).then((res) => {
    if (res.data.code == 200) {
      ElMessage.success("修改成功");
      loadTypeTree();
    } else {
      ElMessage.error(res.data.msg);
    }
  });
  console.log(row);
};

const loadTypeTree = () => {
  console.log("loadTypeTree", typeTree);
  getTypeTree().then((res) => {
    if (res.data.code == 200) {
      typeTree.value = res.data.data;
    }
  });
};
onMounted(() => {
  loadTypeTree();
});
</script>

<style scoped>
.avatar-uploader .avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
  border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  text-align: center;
}
</style>
